package com.wenhuitec.productlib.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.gzwh.weblib.bean.NzProductCategory;
import com.gzwh.weblib.bean.Result;
import com.gzwh.weblib.utils.MyObserver;
import com.gzwh.weblib.utils.RequestUtils;
import com.wenhuitec.productlib.R;
import com.wenhuitec.productlib.entity.ProductFoods;
import com.wenhuitec.productlib.ui.adapter.CategoryManagerAdapter;
import com.wenhuitec.productlib.ui.adapter.MenuCategoryAdapter;
import com.wenhuitec.productlib.ui.adapter.MenuFoodAdapter;
import com.wenhuitec.sysui.TitleBuilder;

import java.util.ArrayList;
import java.util.List;

public class FoodManagerActivity extends AppCompatActivity {

    private Context mContext;

    private RecyclerView leftCateRv;

    private RecyclerView rightFoodRv;

    private TextView tv_header;

    private MenuCategoryAdapter menuCategoryAdapter;

    private MenuFoodAdapter menuFoodAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_manager);
        mContext = this;
        new TitleBuilder(this)
                .setTitleText("菜品管理")
//                .setLeftIco(R.drawable.yp)
                .setLeftIcoListening((view) -> finish());
        findViewById(R.id.categoryManagerRv).setOnClickListener(view -> {
            startActivity(new Intent(mContext, CategoryManagerActivity.class));
        });
        findViewById(R.id.addMenuFoodBut).setOnClickListener(view -> {
            startActivity(new Intent(mContext, FoodsEditActivity.class));
        });
        leftCateRv = findViewById(R.id.leftCateRv);
        rightFoodRv = findViewById(R.id.rightFoodRv);
        tv_header = findViewById(R.id.tv_header);
    }

    @Override
    public void onResume() {
        super.onResume();
        RequestUtils.getCategoryList(mContext, new MyObserver<List<NzProductCategory>>(mContext, false) {
            @Override
            public void onSuccess(Result<List<NzProductCategory>> result) {
                leftCateRv.setLayoutManager(new LinearLayoutManager(mContext));//设置布局管理器
                menuCategoryAdapter = new MenuCategoryAdapter(mContext, result.getResult(), dTreeCategoryModel -> {
                    tv_header.setText(dTreeCategoryModel.getCategoryName());
                    menuFoodAdapter.setmList(new ArrayList<>());
                    showFoods(dTreeCategoryModel.getId());
                });
                leftCateRv.setAdapter(menuCategoryAdapter);
                if (result.getResult().size() > 0) {
                    showFoods(result.getResult().get(0).getId());
                    tv_header.setText(result.getResult().get(0).getCategoryName());
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }

    private void showFoods(String categoryId) {
        RequestUtils.getProductList(mContext, categoryId, new MyObserver<JSONArray>(mContext, false) {
            @Override
            public void onSuccess(Result<JSONArray> result) {
                System.out.println("获取：" + result.toString());

                List<ProductFoods> productFoodsList = result.getResult().toJavaList(ProductFoods.class);
                rightFoodRv.setLayoutManager(new LinearLayoutManager(mContext));//设置布局管理器
                menuFoodAdapter = new MenuFoodAdapter(mContext, productFoodsList, (foodId, menuFoodModel) -> {
                    FoodsEditActivity.startAct(mContext, menuFoodModel);
                });
                rightFoodRv.setAdapter(menuFoodAdapter);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }
}
